package com.itxiaowu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itxiaowu.dao.OrderSettingDao;
import com.itxiaowu.pojo.OrderSetting;
import com.itxiaowu.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    /**
     * 实现报表文件上传
     *
     * @param orderSettingList
     */
    @Override
    public void upload(List<OrderSetting> orderSettingList) {

        //判断是否有数据
        if(orderSettingList.size() > 0 && orderSettingList != null) {

            //1:对文件日期进行遍历查询,判断是否存在
            for (OrderSetting orderSetting : orderSettingList) {

                OrderSetting findByOrderSetting = orderSettingDao.findByDate(orderSetting.getOrderDate());
                if(findByOrderSetting == null){
                    //2:不存在则进行添加操作  -->日期
                    orderSettingDao.addOrderSetting(orderSetting);
                }else{
                    //3:存在则进行修改操作
                        //拿到主键值
                    Integer id = findByOrderSetting.getId();
                    orderSetting.setId(id);
                    orderSettingDao.updateOrderSetting(orderSetting);
                }
            }

        }
    }

    /**
     * 根据开始和结束条件查询数据
     *
     * @param start
     * @param end
     */
    @Override
    public List<Map<String, Object>> getMsgByMonth(String start, String end) {

        Map<String,Object> map = new HashMap<>();

        //将数据添加进入map集合中来进行处理
        map.put("start",start);
        map.put("end",end);

        //调用dao层的方法
        List<OrderSetting> ordderSettingList = orderSettingDao.getMsgByMonth(map);

        /*前端所需数据格式: { date: 8, number: 120, reservations: 1 } ,需要转换*/
        List<Map<String, Object>> list = new ArrayList<>();
        if(ordderSettingList.size() > 0 && ordderSettingList != null) {


            for (OrderSetting orderSetting : ordderSettingList) {
                Map<String, Object> hash = new HashMap<>();

                //将数据截取获得所需数据
                String formatDate = new SimpleDateFormat("yyyy-MM-dd").format(orderSetting.getOrderDate());
                String day = formatDate.substring(formatDate.lastIndexOf("-") + 1);

                hash.put("date", day);
                hash.put("number", orderSetting.getNumber());
                hash.put("reservations", orderSetting.getReservations());

                //封装到list集合中返回
                list.add(hash);
            }

            System.out.println("个数是:" + list.size());
        }

        return list;
    }

    /**
     * 修改属性
     *
     * @param orderSetting
     */
    @Override
    public void setNumber(OrderSetting orderSetting) {

        orderSettingDao.updateOrderSetting(orderSetting);

    }

    //截取示范
//    public static void main(String[] args) {
//
//        String str = "2019-02-31";
//        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        System.out.println(format);
//        String sub = str.substring(str.lastIndexOf("-") + 1);
//        System.out.println(sub);
//
//    }
}
